// Decompiled by JAD v1.5.4.2. Copyright 1997-98 Pavel Kouznetsov.
// JAD Home Page:      http://web.unicom.com.cy/~kpd/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   Zoop.java

import java.applet.Applet;
import java.awt.*;

class ZoopDisplay
{

    ZoopDisplay(Applet applet)
        throws InterruptedException
    {
        rgCells = new byte[20][14];
        theApplet = applet;
        imageOffScreen = theApplet.createImage(24, 24);
        gOffScreen = imageOffScreen.getGraphics();
        ipCursors = new ImagePalette(theApplet, "cursors.gif");
        rgColor[0][0] = rgColor[0][1] = rgColor[0][2] = rgColor[0][3] = Color.black;
        rgColor[0][4] = rgColor[0][5] = Color.white;
        rgColor[1][0] = rgColor[1][1] = new Color(0, 255, 0);
        rgColor[1][2] = rgColor[1][3] = new Color(0, 0, 128);
        rgColor[1][4] = new Color(128, 0, 0);
        rgColor[1][5] = new Color(128, 128, 0);
        rgColor[2][0] = rgColor[2][3] = Color.white;
        rgColor[2][1] = rgColor[2][2] = Color.black;
        rgColor[2][4] = rgColor[2][5] = Color.blue;
        InitGame();
        displayScore();
    }

    public void GameOver()
    {
        bKeepPlaying = false;
        paintAll(theApplet.getGraphics());
    }

    public void InitGame()
    {
        rwChomper = 5;
        colChomper = 8;
        bDirectionChomper = 0;
        iScore = 0;
        for(int i = 5; i < 9; i++)
        {
            for(int j = 8; j < 12; j++)
                setCell(j, i, (byte)0);

        }

        setCell(8, 5, (byte)32);
    }

    public synchronized boolean FPiecesRemaining()
    {
        return iPiecesRemaining > 0;
    }

    public synchronized void setPiecesRemaining(int i)
    {
        iPiecesRemaining = i;
        displayScore();
    }

    private void displayScore()
    {
        int i = iScore;
        for(int j = 6; j >= 0; j--)
        {
            setCell(j, 1, (byte)(0x10 | i % 10));
            i /= 10;
        }

        int k = iPiecesRemaining <= 0 ? 0 : iPiecesRemaining;
        for(int l = 19; l >= 16; l--)
        {
            setCell(l, 1, (byte)(0x10 | k % 10));
            k /= 10;
        }

    }

    public synchronized void ShowLost()
    {
        byte byte0 = (byte)(rgCells[colChomper][rwChomper] & 0x3);
        byte byte1 = (byte)(rgCells[colChomper][rwChomper] >> 2 & 0x3);
        for(int i = 0; i < 150; i++)
        {
            byte1 = (byte)(byte1 + 1 & 0x3);
            setCell(colChomper, rwChomper, (byte)(0x20 | byte0 | byte1 << 2));
            try
            {
                Thread.sleep(25L);
            }
            catch(InterruptedException ex) {}
        }

    }

    public void moveChomper(byte byte0)
    {
        if(!bKeepPlaying || iPromptMode == 0)
            return;
        byte byte1 = (byte)(rgCells[colChomper][rwChomper] & 0x3);
        setCell(colChomper, rwChomper, (byte)0);
        switch(byte0)
        {
        default:
            break;

        case 0: /* '\0' */
            if(colChomper > 8)
                colChomper--;
            break;

        case 1: /* '\001' */
            if(rwChomper > 5)
                rwChomper--;
            break;

        case 2: /* '\002' */
            if(colChomper < 11)
                colChomper++;
            break;

        case 3: /* '\003' */
            if(rwChomper < 8)
                rwChomper++;
            break;

        }
        setCell(colChomper, rwChomper, (byte)(0x20 | byte1 | byte0 << 2));
        bDirectionChomper = byte0;
    }

    public synchronized void fire()
    {
        if(!bKeepPlaying || iPromptMode == 0)
            return;
        int i = colChomper;
        int j = rwChomper;
        boolean flag = true;
        int k = 0;
        int l = 100;
        while(flag) 
        {
            int i1 = colChomper + DColFromDirection[bDirectionChomper];
            int j1 = rwChomper + DRwFromDirection[bDirectionChomper];
            try
            {
                Thread.sleep(0L, 5);
            }
            catch(InterruptedException ex) {}
            if(j1 < 0 || j1 > 13 || i1 < 0 || i1 > 19)
            {
                byte byte0 = (byte)(rgCells[colChomper][rwChomper] & 0x3);
                bDirectionChomper = (byte)((bDirectionChomper + 2) % 4);
                setCell(colChomper, rwChomper, (byte)(0x20 | bDirectionChomper << 2 | byte0));
            }
            else
            if(rgCells[i1][j1] <= 1)
            {
                byte byte1 = rgCells[colChomper][rwChomper];
                setCell(colChomper, rwChomper, (byte)0);
                setCell(i1, j1, byte1);
                colChomper = i1;
                rwChomper = j1;
            }
            else
            if((rgCells[i1][j1] & 0x3) == (rgCells[colChomper][rwChomper] & 0x3))
            {
                byte byte2 = rgCells[colChomper][rwChomper];
                setCell(colChomper, rwChomper, (byte)0);
                setCell(i1, j1, byte2);
                colChomper = i1;
                rwChomper = j1;
                k += l;
                l += 100;
                iPiecesRemaining--;
            }
            else
            {
                bDirectionChomper = (byte)((bDirectionChomper + 2) % 4);
                byte byte3 = (byte)(0x20 | bDirectionChomper << 2 | rgCells[i1][j1] & 0x3);
                setCell(i1, j1, (byte)(rgCells[i1][j1] & 0xfc | rgCells[colChomper][rwChomper] & 0x3));
                setCell(colChomper, rwChomper, byte3);
            }
            if(colChomper == i && rwChomper == j)
                flag = false;
        }

        iScore += k;
        if((bDirectionChomper & 0x1) == 0)
        {
            if(k == 3600)
                iScore += 10000;
        }
        else
        if(k == 1500)
            iScore += 5000;
        displayScore();
    }

    public void clearBoard()
    {
        for(int i = 5; i < 9; i++)
        {
            for(int j = 0; j < 8; j++)
                rgCells[j][i] = 0;

            for(int l = 12; l < 20; l++)
                rgCells[l][i] = 0;

        }

        for(int k = 8; k < 12; k++)
        {
            for(int i1 = 0; i1 < 5; i1++)
                rgCells[k][i1] = 0;

            for(int j1 = 9; j1 < 14; j1++)
                rgCells[k][j1] = 0;

        }

    }

    public synchronized void setColors(int i)
    {
        iLevel = i;
        paintAll(theApplet.getGraphics());
    }

    public synchronized void setCell(int i, int j, byte byte0)
    {
        rgCells[i][j] = byte0;
        paint1(theApplet.getGraphics(), i, j);
    }

    public synchronized byte ContentsFromChannel(byte byte0)
    {
        return rgCells[ColFromChannel[byte0]][RwFromChannel[byte0]];
    }

    public synchronized boolean InsertPiece(byte byte0, byte byte1)
    {
        int i = RwFromChannel[byte0];
        int j = ColFromChannel[byte0];
        int k = DirectionFromChannel[byte0];
        int l = BreachRwFromChannel[byte0];
        int i1 = BreachColFromChannel[byte0];
        if(rgCells[j][i] > 1)
            switch(k)
            {
            default:
                break;

            case 0: /* '\0' */
                for(int j1 = 11; j1 < 19; j1++)
                    if(j1 != 11 || rgCells[j1 + 1][i] > 1)
                        setCell(j1, i, rgCells[j1 + 1][i]);

                break;

            case 1: /* '\001' */
                for(int k1 = 8; k1 < 13; k1++)
                    if(k1 != 8 || rgCells[j][k1 + 1] > 1)
                        setCell(j, k1, rgCells[j][k1 + 1]);

                break;

            case 2: /* '\002' */
                for(int l1 = 8; l1 > 0; l1--)
                    if(l1 != 8 || rgCells[l1 - 1][i] > 1)
                        setCell(l1, i, rgCells[l1 - 1][i]);

                break;

            case 3: /* '\003' */
                for(int i2 = 5; i2 > 0; i2--)
                    if(i2 != 5 || rgCells[j][i2 - 1] > 1)
                        setCell(j, i2, rgCells[j][i2 - 1]);

                break;

            }
        setCell(j, i, (byte)(0x30 | byte1 | k << 2));
        bKeepPlaying = (rgCells[i1][l] & 0x30) != 48;
        return bKeepPlaying;
    }

    public synchronized void paint1(Graphics g, int i, int j)
    {
        if(i > 7 && i < 12 && j > 4 && j < 9)
            gOffScreen.setColor(rgColor[iLevel][4 + (i & 0x1)]);
        else
            gOffScreen.setColor(rgColor[iLevel][(j & 0x1) << 1 | i & 0x1]);
        gOffScreen.fillRect(0, 0, 24, 24);
        if(rgCells[i][j] == 0)
        {
            if(i >= 8 && i <= 11 || j >= 5 && j <= 8)
                ipCursors.drawPiece(gOffScreen, 24, 0, 24, 24, 0, 0);
            else
                ipCursors.drawPiece(gOffScreen, 0, 0, 24, 24, 0, 0);
        }
        else
        {
            ipCursors.drawPiece(gOffScreen, 24 * (rgCells[i][j] & 0xf), 24 * ((rgCells[i][j] & 0x30) >> 4), 24, 24, 0, 0);
        }
        g.drawImage(imageOffScreen, i * 24, j * 24, 24, 24, theApplet);
    }

    public synchronized void paintAll(Graphics g)
    {
        if(iPromptMode == 0)
        {
            g.setColor(Color.white);
            for(int i = 1; i < 14; i += 2)
                g.fillRect(0, i * 24, 480, 24);

            g.setColor(Color.black);
            for(int k = 0; k < 14; k += 2)
                g.fillRect(0, k * 24, 480, 24);

            if(iPromptModeLevel == 0)
            {
                g.setFont(new Font("Helvetica", 1, 22));
                g.drawString("WARNING! WARNING! WARNING!", 24, 45);
                g.drawString("Proceeding to the next level may cause", 24, 93);
                g.drawString("society and may impair your motor skills.", 24, 141);
                g.drawString("Do you want to go on? (Press Y or N).", 24, 285);
                g.setColor(Color.white);
                g.drawString("an inability to function within", 24, 117);
                return;
            }
            else
            {
                g.setFont(new Font("Helvetica", 1, 22));
                g.drawString("GAME OVER!", 4, 45);
                g.drawString("Do you want to play again? (Press Y or N).", 4, 285);
                g.setFont(new Font("Helvetica", 1, 15));
                g.drawString("Before you play again:", 4, 91);
                g.drawString("Score more points by getting rid of several objects in one shot.", 4, 139);
                g.drawString("Switch colors to build rows or columns that are the same color.", 4, 187);
                g.drawString("different color than the centerpiece.", 4, 235);
                g.setColor(Color.white);
                g.drawString("Hint:", 4, 115);
                g.drawString("Strategy:", 4, 163);
                g.drawString("Remember, you can switch colors by shooting a shape that is a", 4, 211);
                return;
            }
        }
        for(int j = 0; j < 4; j++)
        {
            for(int l = j; l < 20; l += 4)
            {
                for(int i1 = 0; i1 < 14; i1++)
                    paint1(g, l, i1);

            }

        }

    }

    public synchronized void paint(Graphics g)
    {
        Point point = new Point(0, 0);
        Point point1 = new Point(0, 0);
        Rectangle rectangle = g.getClipRect();
        if(iPromptMode == 0 || rectangle.width == 480 && rectangle.height == 336)
        {
            paintAll(g);
            return;
        }
        point.x = (int)Math.floor(rectangle.x / 24D);
        point.y = (int)Math.floor(rectangle.y / 24D);
        point1.x = (int)Math.ceil(rectangle.width / 24D);
        point1.y = (int)Math.ceil(rectangle.height / 24D);
        for(int i = point.x; i < point.x + point1.x; i++)
        {
            for(int j = point.y; j < point.y + point1.y; j++)
                paint1(g, i, j);

        }

    }

    public synchronized void SetPromptMode(int i)
    {
        iPromptMode = 0;
        iPromptModeLevel = i;
        paintAll(theApplet.getGraphics());
    }

    public synchronized int CheckPromptMode()
    {
        return iPromptMode;
    }

    public synchronized void PromptKey(int i)
    {
        if(iPromptMode == 0)
            iPromptMode = i;
    }

    private static final int dxCell = 24;
    private static final int dyCell = 24;
    private static final int cCol = 20;
    private static final int cRw = 14;
    private static boolean bKeepPlaying = true;
    private static Color rgColor[][] = new Color[3][6];
    private static int iLevel;
    private static Applet theApplet;
    private static Image imageOffScreen;
    private static Graphics gOffScreen;
    private static ImagePalette ipCursors;
    private byte rgCells[][];
    private static int rwChomper;
    private static int colChomper;
    private static byte bDirectionChomper;
    private static int iScore;
    private static int iPiecesRemaining;
    private int DRwFromDirection[] = {
        0, -1, 0, 1
    };
    private int DColFromDirection[] = {
        -1, 0, 1, 0
    };
    private int RwFromChannel[] = {
        5, 6, 7, 8, 5, 6, 7, 8, 0, 0, 
        0, 0, 13, 13, 13, 13
    };
    private int ColFromChannel[] = {
        0, 0, 0, 0, 19, 19, 19, 19, 8, 9, 
        10, 11, 8, 9, 10, 11
    };
    private int DirectionFromChannel[] = {
        2, 2, 2, 2, 0, 0, 0, 0, 3, 3, 
        3, 3, 1, 1, 1, 1
    };
    private int BreachRwFromChannel[] = {
        5, 6, 7, 8, 5, 6, 7, 8, 5, 5, 
        5, 5, 8, 8, 8, 8
    };
    private int BreachColFromChannel[] = {
        8, 8, 8, 8, 11, 11, 11, 11, 8, 9, 
        10, 11, 8, 9, 10, 11
    };
    private static int iPromptMode = 1;
    private static int iPromptModeLevel;

}
